<template>
  <section>
    <el-form :model="target">
      <el-form-item>
        <el-row class="variable-box">
          <el-col :span="8"
                  v-if="false">
            <el-select :value="source.type"
                       @input="handleInput('type', arguments)">
              <el-option v-for="item in typeOptions"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-col>
          <el-col :span="9">
            <el-input :value="source.name"
                      placeholder="变量名"
                      @input="handleInput('name', arguments)"
                      @blur="handleBlur"></el-input>
          </el-col>
          <el-col :span="15">
            <el-input :value="source.value"
                      placeholder="变量值/表达式"
                      @input="handleInput('value', arguments)"></el-input>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </section>
</template>

<script>
export default {
  name: "VariableEdit",
  props: {
    value: {
      type: Object,
      default () {
        return {};
      }
    }
  },
  data () {
    return {
      target: {},
      typeOptions: [
        {
          label: "常量",
          value: 1
        },
        {
          label: "函数",
          value: 2
        },
        {
          label: "表达式",
          value: 3
        },
        {
          label: "字段名",
          value: 4
        }
      ]
    };
  },
  computed: {
    merged () {
      return { ...this.source, ...this.target };
    },
    source () {
      return this.value || {};
    }
  },
  methods: {
    handleInput (name, [value]) {
      this.$set(this.target, name, value);
      this.$emit("input", this.merged);
    },
    handleBlur () {
      if (!this.merged.value && this.merged.name) {
        this.merged.value = this.merged.name;
      }
      this.$emit("input", this.merged);
    }
  }
};
</script>

<style scoped>
::v-deep .el-input__inner {
  border: 0px solid var(--prev-border-color-grey);
  border-radius: 0px !important;
}

.variable-box > div {
  border: 1px solid var(--prev-border-color-grey);
}

.variable-box > div + div {
  border-left: 0px solid var(--prev-border-color-grey);
}
</style>
